package com.software.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.bean.CartBean;
import com.software.bean.CustomerInfoBean;
import com.software.bean.ProductBean;
import com.software.dto.AddCartDto;
import com.software.dto.DeleteCartDto;
import com.software.mapper.CartMapper;
import com.software.mapper.CustomerInfoMapper;
import com.software.mapper.ProductMapper;
import com.software.service.CartService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2024-04-10
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, CartBean> implements CartService {

    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CartMapper cartMapper;

    /**
     * @description:加入购物车功能
     * @author: wyj
     * @date: 2024/4/10 12:00
     * @param:
     * @return:
     */
    @Override
    public Integer addCart(AddCartDto addCartDto) {
        AssertUtil.isTrue(addCartDto.getCustomerId() == null, "用户id不能为空");
        AssertUtil.isTrue(addCartDto.getPid() == null, "商品id不能为空");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(addCartDto.getCustomerId());
        AssertUtil.isTrue(customerInfoBean == null, "用户不存在");
        ProductBean productBean = productMapper.selectById(addCartDto.getPid());
        AssertUtil.isTrue(productBean == null, "商品不存在");
        AssertUtil.isTrue(productBean.getProductStatus() != 1, "商品已失效");
        // 判断是否已加入购物车
        CartBean selectByCidAndPid = cartMapper.selectByCidAndPid(addCartDto.getCustomerId(), addCartDto.getPid());
        AssertUtil.isTrue(selectByCidAndPid != null, "商品已加入购物车");


        CartBean cartBean = new CartBean();
        cartBean.setCustomerId(addCartDto.getCustomerId());
        cartBean.setProductId(addCartDto.getPid());
        cartBean.setTime(new Date());
        cartBean.setCount(1);
        cartBean.setStatus(1);
        int insert = cartMapper.insert(cartBean);
        return insert == 1 ? 1 : 0;
    }

    /**
     * @description:前端获取购物车列表
     * @author: wyj
     * @date: 2024/4/10 12:58
     * @param:
     * @return:
     */
    @Override
    public List<CartBean> getCartList(Integer customerId) {
        AssertUtil.isTrue(customerId == null, "参数异常");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(customerId);
        AssertUtil.isTrue(customerInfoBean == null, "用户不存在");
        List<CartBean> cartList = cartMapper.selectByCustomerId(customerId);
        return cartList;
    }

    /**
     * @description:购物车删除商品
     * @author: wyj
     * @date: 2024/4/10 18:41
     * @param:
     * @return:
     */
    @Override
    public Integer deleteCartProductById(DeleteCartDto deleteCartDto) {
        AssertUtil.isTrue(deleteCartDto.getCid() == null, "用户id不能为空");
        AssertUtil.isTrue(deleteCartDto.getCartId() == null, "购物车id不能为空");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(deleteCartDto.getCid());
        AssertUtil.isTrue(customerInfoBean == null, "用户不存在");
        CartBean cartBean = cartMapper.getCartBeanByCartIdAndCustomerId(deleteCartDto.getCartId(), deleteCartDto.getCid());
        AssertUtil.isTrue(cartBean == null, "购物车商品不存在");
        int i = cartMapper.deleteById(cartBean);
        return i == 1 ? 1 : 0;
    }
}
